package com.share.rabbitmq.message;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.share.rabbitmq.RabbitMQConnection;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @desc:
 * @author:caifan
 * @date:2020/4/4
 */
public class MessageProducer {

    public static void main(String[] args) throws Exception {
        Connection connect = RabbitMQConnection.connect();

        //创建channel
        Channel channel = connect.createChannel();

        Map<String, Object> headerMap = new HashMap<>();
        headerMap.put("city", "guangzhou");
        headerMap.put("nickname", "share");

        AMQP.BasicProperties basicProperties = new AMQP.BasicProperties().builder()
                .deliveryMode(2) //存储模式 1:不持久化 2:持久化 MessageDeliveryMode
                .contentType("application/json")
                .expiration("10000")//过期时间
                .contentEncoding("utf-8")
                .correlationId(UUID.randomUUID().toString()) //用于可靠性投递
                .headers(headerMap)
                .build();

        //通过channel发消息
        for (int i=0;i<5;i++) {
            String message = "{\"greet\":\"hello\",\"country\":\"中国\"}";
//            channel.basicPublish("share.directExchange", "share.directExchange.key", basicProperties, message.getBytes());
            channel.basicPublish("my.directExchange", "my.directExchange.key", basicProperties, message.getBytes());
        }
        channel.close();
        connect.close();
    }
}
